package com.sgcc.operate.controller;

import java.util.HashMap;
import java.util.List;
import java.io.IOException;
import java.util.Map;
import javax.servlet.http.HttpServletResponse;

import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.sgcc.operate.domain.vo.MonsysMenuDataDto;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.sgcc.common.log.annotation.Log;
import com.sgcc.common.log.enums.BusinessType;
import com.sgcc.common.security.annotation.PreAuthorize;
import com.sgcc.operate.domain.OMonsysMenu;
import com.sgcc.operate.service.IOMonsysMenuService;
import com.sgcc.common.core.web.controller.BaseController;
import com.sgcc.common.core.web.domain.AjaxResult;
import com.sgcc.common.core.utils.poi.ExcelUtil;
import com.sgcc.common.core.web.page.TableDataInfo;

/**
 * 功能菜单明细Controller
 * 
 * @author sgcc
 * @date 2021-04-10
 */
@RestController
@RequestMapping("/monsysmenu")
public class OMonsysMenuController extends BaseController
{
    @Autowired
    private IOMonsysMenuService oMonsysMenuService;

    /**
     * 查询功能菜单明细列表
     */
    //@PreAuthorize(hasPermi = "operate:monsysmenu:list")
    @GetMapping("/list")
    public TableDataInfo list(OMonsysMenu oMonsysMenu)
    {
        startPage();
        List<OMonsysMenu> list = oMonsysMenuService.selectOMonsysMenuList(oMonsysMenu);
        return getDataTable(list);
    }

    /**
     * 导出功能菜单明细列表
     */
    //@PreAuthorize(hasPermi = "operate:monsysmenu:export")
    @Log(title = "功能菜单明细", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, OMonsysMenu oMonsysMenu) throws IOException
    {
        List<OMonsysMenu> list = oMonsysMenuService.selectOMonsysMenuList(oMonsysMenu);
        ExcelUtil<OMonsysMenu> util = new ExcelUtil<OMonsysMenu>(OMonsysMenu.class);
        util.exportExcel(response, list, "monsysmenu");
    }

    /**
     * 获取功能菜单明细详细信息
     */
    //@PreAuthorize(hasPermi = "operate:monsysmenu:query")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") String id)
    {
        return AjaxResult.success(oMonsysMenuService.selectOMonsysMenuById(id));
    }

    /**
     * 新增功能菜单明细
     */
    //@PreAuthorize(hasPermi = "operate:monsysmenu:add")
    @Log(title = "功能菜单明细", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody OMonsysMenu oMonsysMenu)
    {
        // 获取排序号，最末节点排序号+30
        if (StringUtils.isBlank(oMonsysMenu.getId())){
//			List<OMonsysMenu> list = Lists.newArrayList();
////			List<OMonsysMenu> sourcelist = oMonsysMenuService.findAllMenu();
////			OMonsysMenu.sortList(list, sourcelist, oMonsysMenu.getParentId(), false);
////			if (list.size() > 0){
////				oMonsysMenu.setSort(list.get(list.size()-1).getSort() + 30);
////			}
            OMonsysMenu seach=new OMonsysMenu();
            List<OMonsysMenu> sourcelist = oMonsysMenuService.selectOMonsysMenuList(seach);
            if (sourcelist.size() > 0){
                oMonsysMenu.setSort(sourcelist.get(sourcelist.size()-1).getSort() + 30);
            }else {
                oMonsysMenu.setSort(30L);
            }
        }

        return toAjax(oMonsysMenuService.insertOMonsysMenu(oMonsysMenu));
    }

    /**
     * 修改功能菜单明细
     */
    //@PreAuthorize(hasPermi = "operate:monsysmenu:edit")
    @Log(title = "功能菜单明细", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody OMonsysMenu oMonsysMenu)
    {
        return toAjax(oMonsysMenuService.updateOMonsysMenu(oMonsysMenu));
    }

    /**
     * 删除功能菜单明细
     */
    //@PreAuthorize(hasPermi = "operate:monsysmenu:remove")
    @Log(title = "功能菜单明细", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable String[] ids)
    {
        return toAjax(oMonsysMenuService.deleteOMonsysMenuByIds(ids));
    }


    @GetMapping("monsysMenuDataList")
    public TableDataInfo monsysMenuDataList(MonsysMenuDataDto menu){
        //默认选择查询营销业务应用数据
//		if (org.apache.commons.lang3.StringUtils.isEmpty(query.getMonSysCode())) {
//			query.setMonSysCode("01");
//		}
        String id = menu.getId();
        if (org.apache.commons.lang3.StringUtils.isNotEmpty(id)) {
            OMonsysMenu oMonsysMenu = oMonsysMenuService.selectOMonsysMenuById(id);
            if (null != oMonsysMenu && null != oMonsysMenu.getId()) {
                menu.setIsPar(oMonsysMenu.getIsPar());
            }
        }
        List<MonsysMenuDataDto> list = oMonsysMenuService.monsysMenuDataList(menu);
        return getDataTable2(list);
    }

    /**
     * 查询功能菜单树子集数据
     * @param parentId
     * @return
     */
    @GetMapping( "getChildren")
    public List<OMonsysMenu> getChildren(String parentId){
        if (org.apache.commons.lang3.StringUtils.isEmpty(parentId)) {
            parentId = "1";
        }
        if("-1".equals(parentId)){//如果是-1，没指定任何父节点，就从根节点开始查找
            parentId = "1";
        }
//		return oMonsysMenuService.getChildren(parentId);
        Map<String, Object> condition = new HashMap<>();
        condition.put("parentId", parentId);
        return oMonsysMenuService.getChildrenEx(condition);
    }

    /**
     * 查询功能菜单树数据
     * @param extId
     * @return
     */
    //	@RequiresPermissions("user")
    @GetMapping( "treeData")
    public List<Map<String, Object>> treeData( String extId) {
        List<Map<String, Object>> mapList = Lists.newArrayList();
//		List<OMonsysMenu> list = oMonsysMenuService.findList(new OMonsysMenu());
        List<OMonsysMenu> list = oMonsysMenuService.findListEx(new OMonsysMenu());
        for (int i=0; i<list.size(); i++){
            OMonsysMenu e = list.get(i);
            if (StringUtils.isBlank(extId) || (extId!=null && !extId.equals(e.getId()) && e.getParentIds().indexOf(","+extId+",")==-1)){
                Map<String, Object> map = Maps.newHashMap();
                map.put("id", e.getId());
                map.put("text", e.getName());
                map.put("monSysCode", e.getMonSysCode());
                if(StringUtils.isBlank(e.getParentId()) || "1".equals(e.getParentId())){
                    map.put("parent", "#");
                    Map<String, Object> state = Maps.newHashMap();
                    state.put("opened", true);
                    map.put("state", state);
                }else{
                    map.put("parent", e.getParentId());
                }
                mapList.add(map);
            }
        }
        return mapList;
    }
}
